package com.ct.application.shipping.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ct.application.shipping.model.po.SysParameterSavePO;
import com.ct.application.shipping.model.po.SysParameterSearchPO;
import com.ct.application.shipping.model.vo.SysParameterVO;
import com.ct.application.shipping.service.ISysParameterService;
import com.ct.repository.commons.BaseServiceImpl;
import com.ct.shipping.mapper.SysParameterMapper;
import com.ct.shipping.orm.SysParameterDO;
import com.ct.utils.JsonUtil;
import com.ct.utils.exception.ApiException;
import com.ct.utils.result.StatusEnums;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author Auto Generator
 * @since 2023-08-16
 */
@Service
public class SysParameterServiceImpl extends BaseServiceImpl<SysParameterMapper, SysParameterDO> implements ISysParameterService {


    /**
     * 查询平台设置接口搜索并分页
     * @return
     */
    @Override
    public Page<SysParameterVO> findSysParameters(SysParameterSearchPO sysParameterSearchPO){
        LambdaQueryWrapper<SysParameterDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Objects.nonNull(sysParameterSearchPO.getStatus()),SysParameterDO::getStatus,sysParameterSearchPO.getStatus());
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(sysParameterSearchPO.getKeyword()),SysParameterDO::getTitle,sysParameterSearchPO.getKeyword());
        lambdaQueryWrapper.orderByDesc(SysParameterDO::getCreateTime);
        Page<SysParameterDO> page = new Page<>(sysParameterSearchPO.getPageNo(),sysParameterSearchPO.getPageSize());
        Page<SysParameterDO> page1 = this.page(page, lambdaQueryWrapper);
        return tranferPageBo(page1, SysParameterVO.class);
    }

    @Override
    public List<SysParameterVO> findSysParametersList(){
        LambdaQueryWrapper<SysParameterDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysParameterDO::getStatus, StatusEnums.STATUS.getCode());
        return tranferListBo(this.list(lambdaQueryWrapper),SysParameterVO.class);
    }
    /**
     * 保存平台设置
     */
    @Override
    public boolean saveUpdateSysParameter(SysParameterSavePO sysParameterSavePO){
        SysParameterDO sysParameterDO = tranferBo(sysParameterSavePO, SysParameterDO.class);
        if (Objects.nonNull(sysParameterDO)){
            if(Objects.isNull(sysParameterDO.getId())){
                LambdaQueryWrapper<SysParameterDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(SysParameterDO::getCode,sysParameterDO.getCode());
                SysParameterDO one = this.getOne(lambdaQueryWrapper);
                if (Objects.nonNull(one)){
                    throw new ApiException("param_code","参数编号已被占用");
                }
            }
            return this.saveOrUpdate(sysParameterDO);
        }
        return false;
    }

    /**
     * 删除平台设置
     * @param id
     * @return
     */
    @Override
    public boolean deleteSysParameterById(Long id){
        return this.removeById(id);
    }

    /**
     * 根据id获取平台设置信息
     * @param id
     * @return
     */
    @Override
    public SysParameterVO getSysParameterById(Long id){
        SysParameterDO sysParameterDO = this.getById(id);
        if (Objects.nonNull(sysParameterDO)) {
            return tranferBo(sysParameterDO, SysParameterVO.class);
        }
        return null;
    }

    // 状态修改
    @Override
    public boolean updateStatus(Long id,String column,Integer value){
        boolean b =  super.updateStatus(id,column,value);
        return b;
    }

    @Override
    public boolean updateStatusStr(Long id,String column,String value){
        boolean b =  super.updateStatusStr(id,column,value);
        return b;
    }
}
